package zhongbo.javaweb.instagram.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import zhongbo.javaweb.instagram.dto.JsonRespDto;
import zhongbo.javaweb.instagram.dto.TestDto;
import zhongbo.javaweb.instagram.model.Task;
import zhongbo.javaweb.instagram.service.InstagramService;

import java.util.Map;

/**
 * 说明：
 *
 * @author zhongboWork
 * @version 1.0
 * @since 2021-12-21 16:16
 */
@Controller
@RequestMapping("Task")
@Api(value = "任务数据相关接口", tags = {"任务数据相关接口"})
public class TaskController extends BaseController<Task> {

    @Autowired
    private InstagramService instagramService;

    @ResponseBody
    @RequestMapping(value = "/{id}/start", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "启动任务接口")
    public JsonRespDto<String> startTask(@PathVariable("id") String id) {
        boolean start = instagramService.start(id);
        if (!start) {
            return new JsonRespDto<String>().makeError().appendMsg("操作失败");
        }
        return new JsonRespDto<String>().makeSuccess().setData(id);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/pause", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "暂停任务接口")
    public JsonRespDto<String> pauseTask(@PathVariable("id") String id) {
        boolean start = instagramService.pause(id);
        if (!start) {
            return new JsonRespDto<String>().makeError().appendMsg("操作失败");
        }
        return new JsonRespDto<String>().makeSuccess().setData(id);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/resume", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "恢复任务接口")
    public JsonRespDto<String> resumeTask(@PathVariable("id") String id) {
        boolean resume = instagramService.resume(id);
        if (!resume) {
            return new JsonRespDto<String>().makeError().appendMsg("操作失败");
        }
        return new JsonRespDto<String>().makeSuccess().setData(id);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/stop", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "停止任务接口")
    public JsonRespDto<String> stopTask(@PathVariable("id") String id) {
        boolean start = instagramService.stop(id);
        if (!start) {
            return new JsonRespDto<String>().makeError().appendMsg("操作失败");
        }
        return new JsonRespDto<String>().makeSuccess().setData(id);
    }

    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "测试下载接口")
    public JsonRespDto<String> testDownload(@RequestBody TestDto testDto) throws Exception {
        String url = testDto.getUrl();
        String proxy = testDto.getProxy();
        Map<String, String> headers = testDto.getHeaders();
        String getIns = instagramService.testGetIns(url, proxy, headers);
        if (getIns == null) {
            return new JsonRespDto<String>().makeError().appendMsg("操作失败");
        }
        return new JsonRespDto<String>().makeSuccess().setData(getIns);
    }


}
